import {request} from "../utils/request";


/**
 *
 * 查询已注册的数据分页查询
 * @param obj
 * @returns {Promise<AxiosResponse<any>>}
 */

export function fetchHairCardList(query) {
    return request('get', '/admin/hair-card', query)
}


/**
 *
 * 修改卡桊
 * @param hairCard
 * @returns {Promise<AxiosResponse<any>>}
 */
export function editHairCard(hairCard) {
    return request('put', '/admin/hair-card', hairCard)
}

export function activateHairCard(id) {
    return request('put', '/admin/activate?id='+id,)
}

export function addHairCard(hairCard) {
    return request('post', '/admin/hair-card', hairCard)
}

export function deleteHairCard(id) {
    return request('delete', '/admin/hair-card/' + id, {})
}


function disposereq(self, data) {
    console.log(data.message)
    if (data.message.indexOf('401') != -1) {
        self.$message.info("token过期，请重新登录！");
        setTimeout(() => {
            self.$router.push({
                path: '/login'
            })
        }, 1000)

    }
};

/**
 *
 * 下载文件
 */

import axios from 'axios';
import { saveAs } from 'file-saver';
const downloadFile = async (url, fileName) => {
    try {
        const response = await axios({
            url,
            method: 'GET',
            responseType: 'blob', // response type should be blob
        });
        const fileBlob = new Blob([response.data], { type: response.headers['content-type'] });
        const fileUrl = window.URL.createObjectURL(fileBlob);
        saveAs(fileUrl, fileName); // save file to local disk
        window.URL.revokeObjectURL(fileUrl); // release object URL resource
    } catch (error) {
        console.error(error);
    }
}
export {
    downloadFile ,
    disposereq
}
